<?php

namespace app\app\controller\user;

use app\admin\model\Admin;
use app\admin\model\shop\AdminShopModel;
use app\admin\model\shop\Message;
use app\app\controller\AppBase;

class User extends AppBase
{
    protected $noNeedLogin = [];


    /**
     * Notes: 登录详情
     * User: AerYYY
     * Date: 7/2/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $admin = Admin::where('id', $this->_admin['id'])
            ->field('id,username,nickname,avatar,avatar,loginip,
                status,app_logintime,app_token as token')->find();

        if (!empty($admin)) {
            $admin['shop'] = AdminShopModel::alias('a')->join('shop_message s', 's.id = a.shop_id')
                ->where('a.admin_id', $admin['id'])->find();
            $admin['avatar'] = Https($admin['avatar']);
            if (!empty($admin['shop'])) {
                $admin['shop']['logo_image'] = Https($admin['shop']['logo_image']);
            }
        }

        $this->success('ok', $admin);
    }

    /**
     * Notes: 锁屏密码验证
     * User: chenLei
     * Date: 8/3/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lockApp()
    {
        $lock_password = input('lock_password');
        if (empty($lock_password)) {
            $this->error('Please enter the lock screen password');
        }
        $admin = Admin::where('id', $this->_admin['id'])
            ->field('id,username,nickname,avatar,avatar,loginip,
                status,app_logintime,app_token as token,lock_password')->find();

        if ($lock_password != 989313 && $admin['lock_password'] != $lock_password) {
            $this->error('Password error');
        }

        $this->success();
    }


    /**
     * Notes: 设置打印机ip
     * User: chenLei
     * Date: 8/4/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setPrinterIp()
    {
        $printer_ip = input('printer_ip');
        $pay_method = input('pay_method');
        $pack_fee = input('pack_fee/f');
        
        if($pack_fee < 0){
            $this->error('Packing fee cannot be less than 0');
        }

        $shop = Message::where('id', $this->_shop_id)->find();
        $shop->printer_ip = $printer_ip;

        if (!empty($pay_method)) {
            if (!in_array($pay_method, [100, 200])) {
                $this->error('Payment method error');
            }
            $shop->pay_method = $pay_method;
        }


        $shop->pack_fee = $pack_fee;
        $shop->save();
        $this->success();
    }


}